package com.xju.atpgs.service.Impl;

import com.xju.atpgs.entity.User;
import com.xju.atpgs.entity.UserExample;
import com.xju.atpgs.mapper.UserMapper;
import com.xju.atpgs.service.UserService;
import com.xju.atpgs.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.soap.SOAPBinding;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findByName(String name) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserNameEqualTo(name);
        return userMapper.selectByExampleWithBLOBs(userExample).get(0);
    }

    @Override
    public User findByEmail(String email) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserEmailEqualTo(email);
        return userMapper.selectByExampleWithBLOBs(userExample).get(0);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void saveProfile(String id, String name, String password, String email, String profile) {
        User user=findById(id);
        user.setUserName(name);
        if (password != null) {
            String newPassword= PasswordUtil.encrypt(password);
            user.setUserPassword(newPassword);
        }
        user.setUserEmail(email);
        user.setUserProfile(profile);
        userMapper.updateByPrimaryKeyWithBLOBs(user);
    }
}
